package com.base.common.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.base.common.dao.KnowledgeDAO;
import com.base.model.Knowledge;

public class KnowledgeServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String method = (String)req.getParameter("method");
		if(method != null){
			if("load".equals(method)){
				this.load(req, resp);
			}else if("list".equals(method)){
				this.list(req, resp);
			}else if("insert".equals(method)){
				this.insert(req, resp);
			}else if("update".equals(method)){
				this.update(req, resp);
			}else if("delete".equals(method)){
				this.delete(req, resp);
			}else if("forInsert".equals(method)){
				this.forInsert(req, resp);
			}else if("forUpdate".equals(method)){
				this.forUpdate(req, resp);
			}
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(req, resp);
	}

	protected void load(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		String knowledgeId = (String)req.getParameter("knowledgeId");
		KnowledgeDAO dao = new KnowledgeDAO();
		Knowledge entity = null;
		entity = dao.load(knowledgeId);
		if(entity != null){
			req.setAttribute("entity", entity);
			RequestDispatcher rd = req.getRequestDispatcher("/pages/admin/knowledge/knowledgeInfo.jsp");
			rd.forward(req, resp);
		} else{
			resp.sendRedirect("pages/admin/admin.jsp");
			return;
		}
	}
	
	protected void list(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		KnowledgeDAO dao = new KnowledgeDAO();
		List<Knowledge> results = (List<Knowledge>)dao.list();
		if(results != null){
			req.setAttribute("list", results);
		}else{
			req.setAttribute("list", null);
		}
		RequestDispatcher rd = req.getRequestDispatcher("/pages/admin/knowledge/knowledgeManage.jsp");
		rd.forward(req, resp);
	}
	
	protected void insert(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		String knowledgeSubject = (String)req.getParameter("knowledgeSubject");
		String knowledgeContent = (String)req.getParameter("knowledgeContent");
		
		KnowledgeDAO dao = new KnowledgeDAO();
		
		Knowledge entity = new  Knowledge();
		entity.setKnowledgeId(entity.getUUID());
		entity.setKnowledgeSubject(knowledgeSubject);
		entity.setKnowledgeContent(knowledgeContent);
		
		boolean success = dao.insert(entity);
		if(success){
			req.setAttribute("success", "true");
		}else{
			req.setAttribute("success", "false");
		}
		RequestDispatcher rd = req.getRequestDispatcher("/KnowledgeServlet.do?method=list");
		rd.forward(req, resp);
	}
	
	protected void update(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		String knowledgeId = (String)req.getParameter("knowledgeId");
		String knowledgeSubject = (String)req.getParameter("knowledgeSubject");
		String knowledgeContent = (String)req.getParameter("knowledgeContent");
		KnowledgeDAO dao = new KnowledgeDAO();
		
		Knowledge entity = new  Knowledge();
		entity.setKnowledgeId(knowledgeId);
		entity.setKnowledgeSubject(knowledgeSubject);
		entity.setKnowledgeContent(knowledgeContent);
		
		boolean success = dao.update(entity);
		if(success){
			req.setAttribute("success", "true");
		}else{
			req.setAttribute("success", "false");
		}
		RequestDispatcher rd = req.getRequestDispatcher("/KnowledgeServlet.do?method=list");
		rd.forward(req, resp);
	}
	
	protected void delete(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		String knowledgeId = (String)req.getParameter("knowledgeId");
		KnowledgeDAO dao = new KnowledgeDAO();
		
		boolean success = dao.delete(knowledgeId);
		if(success){
			req.setAttribute("success", "true");
		}else{
			req.setAttribute("success", "false");
		}
		RequestDispatcher rd = req.getRequestDispatcher("/KnowledgeServlet.do?method=list");
		rd.forward(req, resp);
	}
	
	protected void forInsert(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		resp.sendRedirect("pages/admin/knowledge/newKnowledge.jsp");
	}
	
	protected void forUpdate(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		String knowledgeId = (String)req.getParameter("knowledgeId");
		KnowledgeDAO dao = new KnowledgeDAO();
		Knowledge entity = null;
		entity = dao.load(knowledgeId);
		if(entity != null){
			req.setAttribute("entity", entity);
			RequestDispatcher rd = req.getRequestDispatcher("/pages/admin/knowledge/updateKnowledge.jsp");
			rd.forward(req, resp);
		} else{
			resp.sendRedirect("pages/admin/admin.jsp");
			return;
		}
	}
}
